import SomeoneMenu from "./SomeoneMenu";
import { TipMag } from "./TipsUI";

export enum SocketType {
    setName = 1,          //填写昵称
    reqHost = 2,          //登录          
    game = 3,             //申请主持游戏  成功或者失败  isSuccess ： 1 成功 0 失败
    wirteResule = 4,      //填写谜底
    exitGame = 5,         //退出游戏
    joinRoom = 6,         //加入房间
    sendResule = 7,       //发送答案
    errorLog = 8,          //提示
    gameend = 9,           //结算
    returnSelfRoom = 10,   //返回自己的房间
    readyGame = 11         //准备
}

export class EventTypeStr {
    public static setName: string = 'setName';
    public static reqHost: string = 'reqHost';
    public static game: string = 'game';
    public static wirteResule: string = 'wirteResule';
    public static exitGame: string = 'exitGame';
    public static joinRoom: string = 'joinRoom';
    public static sendResule: string = 'sendResule';
    public static gameend: string = 'gameend';
    public static returnSelfRoom: string = 'returnSelfRoom';
    public static readyGame: string = 'readyGame';
}


export class SocketMag {

    public static Instance: SocketMag = null;

    public static getInstance(): SocketMag {

        if (!SocketMag.Instance) {
            SocketMag.Instance = new SocketMag()
            SocketMag.Instance.connectServer();
        }
        return SocketMag.Instance;
    }

    public WS: WebSocket = null;
    connectServer() {

        this.WS = new WebSocket("ws://10.10.10.141:3601");
        this.WS.onopen = (event) => {
            SomeoneMenu.Inst.setTip1(true);
        }
        this.WS.onclose = (event) => {
            SomeoneMenu.Inst.setTip1(false);
            SomeoneMenu.Inst.setTip2(false);
            this.WS.close();
        }
        this.WS.onerror = (event) => {
            cc.log(event);
        }
        this.WS.onmessage = this.ReceiveMsg.bind(this);
    }


    ReceiveMsg(evet) {
        console.log('↩ ' + evet.data);
        let msg = JSON.parse(evet.data);

        switch (msg.socketType) {
            case SocketType.setName:
                cc.director.emit(EventTypeStr.setName, msg._data);
                break;
            case SocketType.joinRoom:
                cc.director.emit(EventTypeStr.joinRoom, msg._data);
                break;
            case SocketType.reqHost:
                cc.director.emit(EventTypeStr.reqHost, msg._data);
                break;
            case SocketType.wirteResule:
                cc.director.emit(EventTypeStr.wirteResule, msg._data);
                break;
            case SocketType.game:
                cc.director.emit(EventTypeStr.game, msg._data);
                break;
            case SocketType.sendResule:
                cc.director.emit(EventTypeStr.sendResule, msg._data);
                break;
            case SocketType.errorLog:
                TipMag.In.PopTips(msg.error);
                break;
            case SocketType.gameend:
                cc.director.emit(EventTypeStr.gameend, msg._data);
                break;
            case SocketType.returnSelfRoom:
                cc.director.emit(EventTypeStr.returnSelfRoom, msg._data);
                break;
            case SocketType.readyGame:
                cc.director.emit(EventTypeStr.readyGame, msg._data);
                break;
            default:
                break;
        }
    }
    /**
     * 
     * @param Obj 协议内包含的参数的对象
     * @param AgId SocketType枚举
     */
    SendMsg(Obj: Object, AgId: SocketType) {
        Obj['socketType'] = AgId;
        let msg = JSON.stringify(Obj);
        if (!msg) return;
        if (this.WS.readyState === WebSocket.OPEN) {
            this.WS.send(msg);
            console.log('↪ ' + msg)
            return;
        }
        TipMag.In.PopTips('服务器连接失败')
    }

}


